import { defineConfig, BuildOptions } from "vite";
import vue from "@vitejs/plugin-vue";
import { resolve } from "path";
import Delete from "rollup-plugin-delete";
import dts from "./plugins/dts";
import vitePluginVuedoc, { vueDocFiles } from "vite-plugin-vuedoc";

const bundlingConf: BuildOptions = {
  lib: {
    entry: resolve(__dirname, "./src/m-ui/index.ts"),
    name: "MUI",
    formats: ["es", "umd", "cjs"],
  },

  rollupOptions: {
    plugins: [
      // Rollup generates all the files, then remove what we don't want.
      // @todo: find a better way.
      Delete({
        targets: ["dist/{images,.htaccess,ghspa.js}", "dist/*.{ico,txt,html}"],
        hook: "generateBundle",
      }),
    ],
    // Make sure to externalize deps that shouldn't be bundled into library.
    external: ["vue"],
    output: {
      // Provide global variables to use in the UMD build for externalized deps.
      globals: { vue: "Vue" },
    },
  },
};

const build = process.env.BUNDLE ? bundlingConf : { outDir: "docs" };

export default defineConfig({
  define: {
    "process.env": {
      // ...process.env,
      // VITE_APP_VERSION: process.env.npm_package_version,
    },
  },
  plugins: [
    vitePluginVuedoc({}),
    vue({
      include: [...vueDocFiles],
      template: {
        compilerOptions: {
          whitespace: "preserve",
        },
      },
    }),
    dts(),
  ], // https://vitejs.dev/config/
  css: {
    preprocessorOptions: {
      scss: {
        // additionalData:
        //   '@import "/@/m-ui/scss/common/var";@import "/@/m-ui/scss/mixins/mixin";',
      },
    },
  },
  resolve: {
    alias: {
      "/@": resolve(__dirname, "./src"),
      "@minerui/m-ui": resolve(__dirname, "./src/m-ui/index"),
    },
  },
  build,
});
